package com.milepost.workflow.assignee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.milepost.workflow.assignee.service.AssigneeService;
import com.milepost.workflow.assignee.vo.ZTreeNode;
import com.milepost.workflow.dept.entity.DeptEntity;
import com.milepost.workflow.dept.mapper.DeptMapper;
import com.milepost.workflow.user.entity.UsersEntity;
import com.milepost.workflow.user.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huarf
 * @Description
 * @create 2022-03-24 1:27
 */
@Service
@RequiredArgsConstructor
public class AssigneeServiceImpl implements AssigneeService {

    private final DeptMapper deptMapper;

    private final UserMapper userMapper;

    @Override
    public List<ZTreeNode> getSubNodesByDeptId(String deptId, String userName) {

        List<ZTreeNode> zTreeNodeList = new ArrayList<>();

        //查询部门
        List<DeptEntity> deptEntityList = null;
        if (StringUtils.isBlank(deptId)) {
            //如果部门id为空，则表示获取所有顶级部门，
            deptEntityList = deptMapper.getRootFeptList();
        } else {
            deptEntityList = deptMapper.selectList(new QueryWrapper<DeptEntity>().lambda().eq(DeptEntity::getParentId, deptId));
        }

        for (DeptEntity deptEntity : deptEntityList) {
            //判断部门下是否有子部门和用户
            boolean has = hasSubDeptOrUser(deptEntity, userName);
            if (!has) {
                continue;
            }
            ZTreeNode zTreeNode = new ZTreeNode();
            zTreeNode.setId(deptEntity.getId());
            zTreeNode.setName(deptEntity.getName());
            zTreeNode.setIsUser(false);
            zTreeNode.setIsParent(true);
            zTreeNodeList.add(zTreeNode);
        }

        //查询用户
        List<UsersEntity> userEntityList = userMapper.selectList(new QueryWrapper<UsersEntity>().lambda().eq(UsersEntity::getDeptId, deptId).like(UsersEntity::getName, userName));

        for (UsersEntity userEntity : userEntityList) {
            ZTreeNode zTreeNode = new ZTreeNode();
            zTreeNode.setId(userEntity.getId());
            zTreeNode.setName(userEntity.getName());
            zTreeNode.setIsUser(true);
            zTreeNode.setIsParent(false);
            zTreeNodeList.add(zTreeNode);
        }

        return zTreeNodeList;
    }

    /**
     * 判断部门下是否有子部门和用户
     * @param deptEntity
     * @param userName
     * @return
     */
    private boolean hasSubDeptOrUser(DeptEntity deptEntity, String userName) {

        Long subDeptAmount = deptMapper.selectCount(new QueryWrapper<DeptEntity>().lambda().eq(DeptEntity::getParentId, deptEntity.getId()));
        if (subDeptAmount > 0) {
            return true;
        }

        Long userAmount = userMapper.selectCount(new QueryWrapper<UsersEntity>().lambda().eq(UsersEntity::getDeptId, deptEntity.getId()).like(UsersEntity::getName, userName));
        return userAmount > 0;
    }

}
